import FormTextFieldValidator, { FormTextFieldValidatorArguments } from '../services/form-text-field-validator';

// type ValidateStatus = Parameters<typeof Form.Item>[0]['validateStatus'];

export interface FormItemValidationResult {
  name?: string,
  label?: string,
  value?: any,
  validatorArguments?: FormTextFieldValidatorArguments,
  validateStatus?: string | null;
  errorMsg?: string | null;
}

export interface FormItemValidationResultMapping {
  [key: string]: FormItemValidationResult;
}

export default class FormItemHelper {
  /**
   * Checks if forma validation is successful or failed.
   */
  static isFormValidationSuccessful(validationResultMapping: FormItemValidationResultMapping) {
    for (const key in validationResultMapping) {
      if (validationResultMapping[key].errorMsg !== null) {
        return false;
      }
    }

    return true;
  }

  static validate(validator: FormTextFieldValidator): FormItemValidationResult {
    const errorMsg = validator.validate();

    if (errorMsg) {
      return FormItemHelper.getErrorValidationResult(errorMsg);
    }

    return FormItemHelper.getSuccessValidationResult();
  }

  static getSuccessValidationResult(): FormItemValidationResult {
    return {
      validateStatus: 'success',
      errorMsg: null,
    };
  }

  static getErrorValidationResult(errorMsg: string): FormItemValidationResult {
    return {
      validateStatus: 'error',
      errorMsg: errorMsg,
    };
  }
}